<!-- 维修评价编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="repairEvaluateModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="repairEvaluateModalApp.repairEvaluate"
        :rules="repairEvaluateModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">
<!--          <a-col :lg="6" :md="12" :sm="24" :xs="24">-->
<!--            <a-form-item label="维修单:" name="repairOrderId">-->
<!--              <a-input-->
<!--                v-model:value="repairEvaluateModalApp.repairEvaluate.repairOrderId"-->
<!--                placeholder="请输入维修单"-->
<!--                :maxlength="255"-->
<!--                allow-clear-->
<!--              />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :lg="24" :md="24" :sm="24" :xs="24">
            <a-form-item label="用户:" name="userId">
              <m-entity-select
                :default-value="repairEvaluateModalApp.repairEvaluate.userName"
                v-model:value="repairEvaluateModalApp.repairEvaluate.userId"
                placeholder="请选择用户"
                module="sys"
                show-options-name="nickname"
                entity="user"
                system="1"
                class="ele-fluid"
                :disabled="true"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="24" :md="24" :sm="24" :xs="24">
            <a-form-item label="维修员:" name="repairUserId">
              <m-entity-select
                :default-value="repairEvaluateModalApp.repairEvaluate.repairUserName"
                v-model:value="repairEvaluateModalApp.repairEvaluate.repairUserId"
                placeholder="请选择维修员"
                show-options-name="nickname"
                module="sys"
                entity="user"
                system="1"
                class="ele-fluid"
                :disabled="true"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="24" :md="24" :sm="24" :xs="24">
            <a-form-item label="评价等级:" name="evaluateGrade">
              <a-rate v-model:value="repairEvaluateModalApp.repairEvaluate.evaluateGrade"/>
            </a-form-item>
          </a-col>

          <a-col :lg="24" :md="24" :sm="24" :xs="24">
            <a-form-item label="评价内容:" name="evaluateContent">
              <a-textarea
                v-model:value="repairEvaluateModalApp.repairEvaluate.evaluateContent"
                placeholder="请输入评价内容"
                allow-clear
                :rows="4"
              />
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="repairEvaluateModalApp.display">
                <teleport to="#repairEvaluateEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="repairEvaluateListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(repairEvaluateModalApp.repairEvaluate)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!repairEvaluateModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(repairEvaluateModalApp.repairEvaluate)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {RepairEvaluateService} from "@/views/repair/repairManagement/repairEvaluate/repairEvaluateService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';


import MEntitySelect from "@/components/MEntity/entitySelect";

export default defineComponent({
  components: {
    MEntitySelect,
  },
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let repairEvaluateListApp = inject('repairEvaluateListApp', reactive({}));
    let repairEvaluateGrid = inject('repairEvaluateGrid', reactive({}))
    const repairEvaluateModalApp = reactive({
      id: null,
      repairEvaluate: {},
      isEdit: false,
      display: false
    });
    repairEvaluateModalApp.rules = {
      repairOrderId: [
        {
          required: true,
          type: 'string',
          message: '请输入维修单',
          trigger: 'blur'
        }
      ],
      repairOrderName: [
        {
          required: false,
          type: 'string',
          message: '请输入维修单',
          trigger: 'blur'
        }
      ],
      userName: [
        {
          required: false,
          type: 'string',
          message: '请输入用户',
          trigger: 'blur'
        }
      ],
      userId: [
        {
          required: true,
          type: 'number',
          message: '请输入用户',
          trigger: 'blur'
        }
      ],
      repairUserName: [
        {
          required: false,
          type: 'string',
          message: '请输入维修员',
          trigger: 'blur'
        }
      ],
      repairUserId: [
        {
          required: true,
          type: 'number',
          message: '请输入维修员',
          trigger: 'blur'
        }
      ],
      repairApplicantName: [
        {
          required: false,
          type: 'string',
          message: '请输入报修人',
          trigger: 'blur'
        }
      ],
      repairApplicantId: [
        {
          required: true,
          type: 'number',
          message: '请输入报修人',
          trigger: 'blur'
        }
      ],
      evaluateGrade: [
        {
          required: true,
          type: 'string',
          message: '请输入评价等级',
          trigger: 'blur'
        }
      ],
      evaluateContent: [
        {
          required: false,
          type: 'string',
          message: '请输入评价内容',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      repairEvaluateModalApp.repairEvaluate = reactive({});
      if (routeId) {//路由
        repairEvaluateModalApp.id = routeId;
        repairEvaluateModalApp.display = false;
        findRepairEvaluate(repairEvaluateModalApp.id);
      } else if (repairEvaluateListApp.editModalForEdit) {//修改弹窗
        repairEvaluateModalApp.id = repairEvaluateListApp.currentId;
        repairEvaluateModalApp.display = true;
      } else if (!repairEvaluateListApp.editModalForEdit && repairEvaluateListApp.addModalForEdit) {//新增弹窗
        repairEvaluateModalApp.display = true;
      } else {
        repairEvaluateModalApp.title = "新增学生";
      }
      if (repairEvaluateModalApp.id) findRepairEvaluate(repairEvaluateModalApp.id);
    })

    const findRepairEvaluate = (id) => {
      RepairEvaluateService.findRepairEvaluateForView(id).then((res) => {
        repairEvaluateModalApp.repairEvaluate = res.data;
        //TODO:为编辑准备.
        repairEvaluateModalApp.title =  + repairEvaluateModalApp.repairEvaluate.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        RepairEvaluateService.updateRepairEvaluate(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        RepairEvaluateService.saveRepairEvaluate(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/repair/repairManagement/repairEvaluate`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        repairEvaluateGrid.value.commitProxy("query");
      }, 1500);
      repairEvaluateListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      repairEvaluateListApp,
      repairEvaluateModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
